package com.relytosoft.mizar.analyze.strategy.algorithm.structure.service.impl;

import cn.hutool.json.JSONUtil;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.mizar.analyze.base.constant.MediaCommonConstants;
import com.relytosoft.mizar.analyze.base.entity.pojo.dto.FeatureExtractionReqDto;
import com.relytosoft.mizar.analyze.base.entity.pojo.dto.TaskRequestDto;
import com.relytosoft.mizar.analyze.common.config.properties.MizarAnalyzeProperties;
import com.relytosoft.mizar.analyze.strategy.algorithm.structure.feign.AlgoStructureFeignService;
import com.relytosoft.mizar.analyze.strategy.algorithm.structure.service.StructureService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * StructureServiceImpl
 *
 * @Author SunLi
 * @Date: 2022/5/13 11:10
 * @Version 1.0
 */
@Service
public class StructureServiceImpl implements StructureService {

    @Resource
    private AlgoStructureFeignService algoStructureFeignService;

    @Resource
    private MizarAnalyzeProperties mizarAnalyzeProperties;

    @Override
    public ResponseResult createTask(TaskRequestDto taskRequestDto) {
        try {
            String strUrl = String.format("%1$s://%2$s:%3$s%4$s?%5$s=%6$s", mizarAnalyzeProperties.getProtocol(), mizarAnalyzeProperties.getGatewayIP(), mizarAnalyzeProperties.getGatewayPort(), MediaCommonConstants.ALGORITHM_STRUCTURE_BITMAIN_SERVICE_URL, "key", mizarAnalyzeProperties.getKey());
            String strUploadUrl = String.format("%1$s://%2$s:%3$s%4$s%5$s?%6$s=%7$s", mizarAnalyzeProperties.getProtocol(), mizarAnalyzeProperties.getGatewayIP(), mizarAnalyzeProperties.getGatewayPort(), MediaCommonConstants.ALGORITHM_STRUCTURE_BITMAIN_SERVICE_URL, MediaCommonConstants.ALGORITHM_RECEVICE_METHOD_NAME, "key", mizarAnalyzeProperties.getKey());
            String strStatusUploadUrl = String.format("%1$s://%2$s:%3$s%4$s%5$s?%6$s=%7$s", mizarAnalyzeProperties.getProtocol(), mizarAnalyzeProperties.getGatewayIP(), mizarAnalyzeProperties.getGatewayPort(), MediaCommonConstants.ALGORITHM_STRUCTURE_BITMAIN_SERVICE_URL, MediaCommonConstants.ALGORITHM_STATUS_METHOD_NAME, "key", mizarAnalyzeProperties.getKey());
            return algoStructureFeignService.createTask(new URI(strUrl), JSONUtil.toJsonStr(taskRequestDto), strStatusUploadUrl, strUploadUrl);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ResponseResult stopTask(String taskId) {
        try {
            String strUrl = String.format("%1$s://%2$s:%3$s%4$s?%5$s=%6$s", mizarAnalyzeProperties.getProtocol(), mizarAnalyzeProperties.getGatewayIP(), mizarAnalyzeProperties.getGatewayPort(), MediaCommonConstants.ALGORITHM_STRUCTURE_BITMAIN_SERVICE_URL, "key", mizarAnalyzeProperties.getKey());
            return algoStructureFeignService.stopTask(new URI(strUrl), taskId);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ResponseResult feartureExtraction(FeatureExtractionReqDto featureExtractionReqDto) {
        try {
            String strUrl = String.format("%1$s://%2$s:%3$s%4$s?%5$s=%6$s", mizarAnalyzeProperties.getProtocol(), mizarAnalyzeProperties.getGatewayIP(), mizarAnalyzeProperties.getGatewayPort(), MediaCommonConstants.ALGORITHM_STRUCTURE_BITMAIN_SERVICE_URL, "key", mizarAnalyzeProperties.getKey());
            return algoStructureFeignService.feartureExtraction(new URI(strUrl), JSONUtil.toJsonStr(featureExtractionReqDto));
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }
}
